#include "GameMenu.h"

CGameMenu::CGameMenu(HINSTANCE hInstance, LPCSTR GameName, int iMode, bool bIsWindowed, int iFPS):CGame(hInstance, GameName, iMode, bIsWindowed, iFPS)
{
	_choose = 0;
	_spriteChoose=new CSprite();
	Choosen();
	//LoadResource();
	//instance = 
}

CGameMenu::~CGameMenu()
{

}
//CGameMenu* CGameMenu::Singleton(LPD3DXSPRITE spriteHandler, float sale)
//{
//	if (instance==NULL)
//	{
//		instance =new CGameMenu(spriteHandler, sale);
//	}
//	return instance;
//}
void CGameMenu::LoadResource()
{

	_background = CGame::CreateSurfaceFromFile("Map.bmp");
	HRESULT result = D3DXCreateSprite(mp_Direct3D->GetDirectDevice(), &spriteHandler);

	mp_Direct3D->GetDirectDevice()->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &m_BackBuffer);

	_spriteChoose->CreateSprite(spriteHandler,"Choose.png",150,40,1,3,GetScreenHeight()/320,D3DCOLOR_XRGB(255,255,255));
	
}
void CGameMenu::RenderFrame()
{	
	UpdateFrame();
	mp_Direct3D->GetDirectDevice()->StretchRect(_background,	//from
			NULL,			//which protion
			m_BackBuffer,	//to
			NULL,			//which protion
			D3DTEXF_POINT);
	
	//lock the surface so that the sprite can be draw
	spriteHandler->Begin(D3DXSPRITE_ALPHABLEND);

	mp_Direct3D->GetDirectDevice()->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
	_spriteChoose->Render(300,200+_choose*40);
	spriteHandler->End();

}
void CGameMenu::UpdateFrame()
{
	//_dxInput->Pool_Keyboard();
	_spriteChoose->SetSpriteLine(_choose);
	
	if(KEY_DOWN(DIK_UP)&&_choose==LoadGame)
	{

		Choosen();
		_choose =Start;
		//_sound->Play("MenuChange",false);
		return;
	}
	if(KEY_DOWN(DIK_UP)&&_choose==Quit)
	{

		Choosen();
		_choose =LoadGame;
		//_sound->Play("MenuChange",false);
		return;
	}
	if(KEY_DOWN(DIK_DOWN)&&_choose==Start) 
	{	
		Choosen();
		_choose =LoadGame;
		//_sound->Play("MenuChange",false);
		return;
	}
	if(KEY_DOWN(DIK_DOWN)&&_choose==LoadGame) 
	{	
		Choosen();
		_choose =Quit;
		//_sound->Play("MenuChange",false);
		return;
	}

	if(KEY_DOWN(DIK_RETURN))
	{
		switch(_choose)
		{
		case Start:
			CGame::state=GameState::Play;
			return;
		case LoadGame:
			CGame::state=GameState::Load;
			return;
		case Quit:
			PostQuitMessage(0);
			return;
		}

	}

}
void CGameMenu::Choosen()
{
	switch(_choose)
	{
	case 0:
		_spriteChoose->SetSpriteLine(0);
		break;
	case 2:
		_spriteChoose->SetSpriteLine(1);
		break;
	case 4:
		_spriteChoose->SetSpriteLine(2);
		break;
	}
	
}

//void CGameMenu::LimitItem()
//{
//	if(_choose > 4)
//		_choose = 0;
//	if(_choose < 0)
//		_choose = 4;
//
//
//}
void CGameMenu::Game_End()
{
//	instance=NULL;
}